package com.ruoyi.web.controller.front;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.impl.SmsServerice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ruoyi
 * @date 2025-02-18
 */
@Api(tags = "公共 接口")
@RestController
@RequestMapping("/api/common")
public class FrontCommonController extends BaseController {
    @Autowired
    private SmsServerice smsServerice;
    /**
     * 短信验证码
     */
    @ApiOperation("短信验证码")
    @GetMapping("/smsCode")
    public AjaxResult smsCode(String phone) {
        return AjaxResult.success(smsServerice.sendCode(phone));
    }


    /**
     * 短信验证码
     */
    @ApiOperation("绑定手机号验证码")
    @GetMapping("/bindTheVerificationCodeOfTheMobilePhoneNumber")
    public AjaxResult bindTheVerificationCodeOfTheMobilePhoneNumber(String phone) {
        return AjaxResult.success(smsServerice.bindTheVerificationCodeOfTheMobilePhoneNumber(phone));
    }


    @ApiOperation("验证绑定手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String")})
    @PostMapping("/verifyTheBoundMobilePhoneNumber")
    public AjaxResult verifyTheBoundMobilePhoneNumber(@RequestBody JSONObject codeObject) {
        Long userId= StpUtil.getLoginIdAsLong();
        smsServerice.verifyTheBoundMobilePhoneNumber(codeObject.getStr("phone"),
                codeObject.getStr("code"),userId);
        return AjaxResult.success();
    }

}
